package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.resp.ResponseResult;
import com.bw.entity.TbCai;
import com.bw.entity.TbGood;
import com.bw.service.ITbCaiService;
import com.bw.service.ITbGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ghq
 * @since 2020-05-31
 */
@RestController
@RequestMapping("/cai")
public class TbCaiController {
    @Autowired
    private ITbCaiService caiService;
    @Autowired
    private ITbGoodService goodService;
    @PostMapping("/savecai")
    public ResponseResult savecai(@RequestBody TbCai tbCai){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            TbGood tbGood = goodService.getById(tbCai.getGid());
            if(tbGood.getKucun()<10){
                tbCai.setStatus(0);
                tbCai.setRtime(new Date());
                caiService.saveOrUpdate(tbCai);
                result.setMessage("保存成功");
            }else{
                result.setSuccess(false);
                result.setMessage("只能采购库存量低于10的商品");
            }
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("保存失败");
        }
        return result;
    }
    @GetMapping("/cailist")
    public ResponseResult cailist(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            List<TbCai> list=caiService.findlist();
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败");
        }
        return result;
    }
    @PostMapping("/updatestatus")
    public ResponseResult update(@RequestBody TbCai tbCai){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            caiService.updatestatus(tbCai);
            result.setMessage("修改成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败");
        }
        return result;
    }
}
